package com.ssyc.gsk_master.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.github.aakira.expandablelayout.ExpandableLayout;
import com.github.aakira.expandablelayout.ExpandableLayoutListenerAdapter;
import com.github.aakira.expandablelayout.ExpandableRelativeLayout;
import com.github.aakira.expandablelayout.Utils;
import com.ssyc.common.view.MyGridView;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.activity.MasterFansDetailsActivity;
import com.ssyc.gsk_master.bean.UnitBean;

import java.util.List;

/**
 * 教师端--教学反思--列表适配器
 * Created by Administrator on 2018/6/22
 */

public class MasterFansiRvAdapter extends BaseQuickAdapter<UnitBean.DataBean, BaseViewHolder> {

    private final List<UnitBean.DataBean> data;
    private Context context;

    private SparseBooleanArray expandState = new SparseBooleanArray();

    public MasterFansiRvAdapter(Context context, int layoutResId, @Nullable List<UnitBean.DataBean> data) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
        //设置默认的展开位置,默认第1个条目为展开位置s
        for (int i = 0; i < data.size(); i++) {
            if (i == 0) {
                expandState.append(i, true);
            } else {
                expandState.append(i, false);
            }
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, UnitBean.DataBean item) {
        //单元名称
        TextView tvUnitName = helper.getView(R.id.tv_group);
        tvUnitName.setText("2-" + item.getGrade_id());

        //包含单元名称的整体相对布局
        RelativeLayout rlGroup = helper.getView(R.id.rl_group);
        ImageView iv_arrow = helper.getView(R.id.iv_arrow);

        if ("1".equals(item.getGrade_id())) {
            rlGroup.setBackgroundColor(context.getResources().getColor(R.color.green));
            tvUnitName.setTextColor(context.getResources().getColor(R.color.white));
            iv_arrow.setImageResource(R.drawable.master_up_arrow);
        }

        //折叠布局
        ExpandableRelativeLayout expandableLayout = helper.getView(R.id.expandableLayout);
        expandableLayout.setInterpolator(item.getInterpolator());
        expandableLayout.setExpanded(expandState.get(helper.getLayoutPosition()));

        //包含切换箭头的整体布局
        RelativeLayout buttonLayout = helper.getView(R.id.button);
        expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
            @Override
            public void onPreOpen() {
                createRotateAnimator(buttonLayout, 0f, 180f).start();
                expandState.put(helper.getLayoutPosition(), true);
                //设置背景和字体颜色
                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.green));
                tvUnitName.setTextColor(context.getResources().getColor(R.color.white));
                iv_arrow.setImageResource(R.drawable.master_up_arrow);
            }

            @Override
            public void onPreClose() {
                createRotateAnimator(buttonLayout, 180f, 0f).start();
                expandState.put(helper.getLayoutPosition(), false);

                rlGroup.setBackgroundColor(context.getResources().getColor(R.color.white));
                tvUnitName.setTextColor(context.getResources().getColor(R.color.green));
                iv_arrow.setImageResource(R.drawable.master_down_arrow);
            }
        });

//        buttonLayout.setRotation(expandState.get(helper.getLayoutPosition()) ? 180f : 0f);

        //包含单元名称的整体相对布局

        rlGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                onClickButton(expandableLayout);
            }
        });

        //展示所有课程数据的列表
        MyGridView mGv = helper.getView(R.id.mgv);

        //设置孩子的数据
        List<UnitBean.DataBean.GradeListBean> grade_list = item.getGrade_list();
        MasterGvLessonAdapter gvAdapter = new MasterGvLessonAdapter(context,
                grade_list, R.layout.master_item_gv_children);
        mGv.setAdapter(gvAdapter);

        mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                if (!grade_list.get(pos).isSelected()) {
                    grade_list.get(pos).setSelected(true);
                    gvAdapter.notifyDataSetChanged();
                }
                //跳转到教学与反思详情界面
                Intent intent = new Intent(context, MasterFansDetailsActivity.class);
                intent.putExtra("gradeId", grade_list.get(pos).getGrade_id());
                intent.putExtra("lessonId", grade_list.get(pos).getLesson_id());
                intent.putExtra("classId", "1");
                context.startActivity(intent);

            }
        });
    }


    /**
     * 创建动画的方法
     *
     * @param target
     * @param from
     * @param to
     * @return
     */
    public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
        animator.setDuration(300);
        animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
        return animator;
    }

    /**
     * 折叠布局的切换方法
     *
     * @param expandableLayout
     */
    private void onClickButton(final ExpandableLayout expandableLayout) {
        expandableLayout.toggle();
    }
}